import * as cc from 'cc';
import { PokerConfig } from "../config/PokerConfig";
import { TextureConfig } from '../config/TextureConfig';

export enum Settle {
    add = '_+_', // 加
    subtract = '_-_', // 减
    multiply = '_*_', // 乘
    divide = '_/_', // 除
    modular = '_%_', // 取余
}

cc.Enum(Settle);

export interface Operator {
    value: number,
    settle: Settle,
}

export abstract class Intensify {

    readonly uuid: string;

    constructor(public desc: string, public readonly value?: Operator, public readonly multiple?: Operator) {
        this.uuid = 'I_'.concat(
            (value ? '-v' + value.settle + value.value : ''),
            (multiple ? '-m' + multiple.settle + multiple.value : ''));
    }

    abstract onDraw(poker: cc.Node, widgetComp: cc.Widget): void;

    onSettle(value: number, multiple: number, poker: PokerConfig.EnumPoker): [number, number] {
        let newValue = value;
        if (this.value) {
            switch (this.value.settle) {
                case Settle.add: {
                    newValue += this.value.value;
                    break;
                }
                case Settle.subtract: {
                    newValue -= this.value.value;
                    break;
                }
                case Settle.multiply: {
                    newValue *= this.value.value;
                    break;
                }
                case Settle.divide: {
                    newValue /= this.value.value;
                    break;
                }
                case Settle.modular: {
                    newValue %= this.value.value;
                    break;
                }
            }
        }

        let newMultiple = multiple;
        if (this.multiple) {
            switch (this.multiple.settle) {
                case Settle.add: {
                    newMultiple += this.multiple.value;
                    break;
                }
                case Settle.subtract: {
                    newMultiple -= this.multiple.value;
                    break;
                }
                case Settle.multiply: {
                    newMultiple *= this.multiple.value;
                    break;
                }
                case Settle.divide: {
                    newMultiple /= this.multiple.value;
                    break;
                }
                case Settle.modular: {
                    newMultiple %= this.multiple.value;
                    break;
                }
            }
        }
        return [newValue, newMultiple]
    }
}

export class PokerIntensify {

    readonly poker: PokerConfig.EnumPoker;

    readonly intensify: Intensify[];

    constructor(poker: PokerConfig.EnumPoker, intensify: Intensify[] = []) {
        this.poker = poker;
        this.intensify = intensify;
    }

    get path() {
        return TextureConfig.getTexture(this.poker).path
    }

    draw(poker: cc.Node, widgetComp: cc.Widget) {
        for (const element of this.intensify) {
            element.onDraw(poker, widgetComp);
        }
    }

    toString() {
        return PokerConfig.pokerIcon(this.poker);
    }
}